// page_two/xfsb/xfsb.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    heights: '',
    yuming: yuming,
    title: '消防设备',
    page: 1,
    huan: ['日常检查上报', '设备报备'],
    currtab: 0,
    // rcjc: [{
    //   id: 1,
    //   time: '2021-05-26 17:17:24',
    //   jcx: 'Fhjb',
    //   jcxq: '正常',
    //   jcr: '元元',
    //   bz: '无其他问题'
    // }],
    rcjc: [],
    sbbb: [],
    // sbbb: [{
    //   id: 1,
    //   name: '扣扣',
    //   ggxh: 'Gy1',
    //   pp: 'Fuu',
    //   yxq: '2019-05-26'
    // }]
    problem: [{
        wenti: '轿厢内检查',
        res: '正常（轿厢内标识标牌是否齐全）'
      },
      {
        wenti: '轿厢内检查',
        res: '正常（轿厢内标识标牌是否齐全）'
      },
      {
        wenti: '轿厢内检查',
        res: '正常（轿厢内标识标牌是否齐全）'
      },
      {
        wenti: '轿厢内检查',
        res: '正常（轿厢内标识标牌是否齐全）'
      },
      {
        wenti: '轿厢内检查',
        res: '正常（轿厢内标识标牌是否齐全）'
      },
      {
        wenti: '轿厢内检查',
        res: '正常（轿厢内标识标牌是否齐全）'
      }
    ]
  },
  // 切换
  qies(e) {
    var index = e.currentTarget.dataset.currtab
    this.setData({
      currtab: index,
    })
  },
  // 日常检查上报添加
  jcjia() {
    wx.navigateTo({
      url: '/page_two/rcjcadd/rcjcadd',
    })
  },
  // 设备报备添加
  sbjia() {
    wx.navigateTo({
      url: '/page_two/sbbb/sbbb',
    })
  },
  getsbList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Besall/XfdeviceList', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page
    }, "POST").then(res => {
      wx.hideLoading();
      this.setData({
        sbbb: res.data.data
      })
      for (let i = 0; i < that.data.sbbb.length; i++) {
        that.data.sbbb[i].open = false;
      }
    })
  },
  getrcjcList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Besall/XfdeviceReportList', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page
    }, "POST").then(res => {
      wx.hideLoading();
      this.setData({
        rcjc: res.data.data
      })
      console.log(this.data.rcjc);
      for (let i = 0; i < that.data.rcjc.length; i++) {
        that.data.rcjc[i].open = false;
      }
      this.geteleList();
    })
  },
  // 删除
  shanchu(e) {
    var id = e.currentTarget.dataset.id
    console.log(id)
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.dele(id)
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 删除接口
  dele(id) {
    let that = this
    let u = ''
    if (that.data.currtab == 0) {
      u = 'Besall/XfdeviceReportDel'
      console.log(yuming + u)
    } else if (that.data.currtab == 1) {
      u = 'Besall/XfdeviceDel'
      console.log(yuming + u)
    }
    this.myRequest(yuming + u, {
      id: id
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        if (that.data.currtab == 0) {
          that.getrcjcList();
        } else if (that.data.currtab == 1) {
          that.getsbList();
        }

      }
    })
  },
  // 点击放大
  fangdas(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  kindToggle0(e) {
    const id = e.currentTarget.id
    const rcjc = this.data.rcjc
    for (let i = 0, len = rcjc.length; i < len; ++i) {
      if (rcjc[i].id == id) {
        rcjc[i].open = !rcjc[i].open
      } else {
        rcjc[i].open = false
      }
    }
    this.setData({
      rcjc
    })
  },
  kindToggle1(e) {
    const id = e.currentTarget.id
    const sbbb = this.data.sbbb
    for (let i = 0, len = sbbb.length; i < len; ++i) {
      if (sbbb[i].id == id) {
        sbbb[i].open = !sbbb[i].open
      } else {
        sbbb[i].open = false
      }
    }
    this.setData({
      sbbb
    })
  },
  geteleList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Besall/XfdeviceListall', {
      shopId: wx.getStorageSync('shopId')
    }, 'POST').then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        this.data.rcjc.forEach(item => {
          res.data.data.forEach(items => {
            if (item.pid == items.id) {
              item.wbsb = items.name
            }
          });
        })
        this.setData({
          rcjc: this.data.rcjc
        })
      }
    })
  },
  // 返回
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      mobile: options.mobile,
      shopType: options.shopType,
      title: options.title
    })
    this.getsbList();
    this.getrcjcList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getsbList();
    this.getrcjcList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})